/*
 * Important in AndroidManifest:
	 * 		<uses-feature android:name="android.software.live_wallpaper" />
			<uses-permission android:name="android.permission.BIND_WALLPAPER"></uses-permission>
			<service 
				...  
			 	<meta-data 
			      android:name="android.service.wallpaper" 
			      android:resource="@xml/walldesc"/>	                <- analyze walldesc.xml file in the xml folder 
			</service>
			      
	
 */

package com.misca.livew;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class LiveWall extends WallpaperService {

	    private final Handler mHandler = new Handler();
	    private static int xP=0;	    
	    
	    	   
	    @Override
	    public void onCreate() {
	        super.onCreate();
	    
	    }
	
	    @Override
	    public void onDestroy() {
	        super.onDestroy();
	    }
	
	    @Override
	    public Engine onCreateEngine() {	   
	        return new wallEngine();  //       <O>
		    }
	    
 //|||||||||||||||||||||||||||||||||||||||||||ENGINE||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
	    class wallEngine extends Engine{ //   <O>   
			
    			private Bitmap backgroundImage, c1;			        			       			        			        
		        private boolean mVisible;			        
				final int wallWidth = getResources().getDisplayMetrics().widthPixels * 2;
				int w=wallWidth;
				final int wallHeight = getResources().getDisplayMetrics().heightPixels;					
				Paint paint = new Paint();
					
			       
			//=======================================
			        
		        wallEngine() { // CONSTRUCTOR	<<<<<<<<<<<<<<<<<<<			            			            			        	
		        				        	 
			        }

		        private final Runnable mDrawWall = new Runnable() { //=========== RUN
			            public void run() {
			            	engine();
			                drawFrame();
			            }
			        };

			    void engine()
			    {
			    	if(w<-100) w=wallWidth;
			    	else w--;
			    }
			//====================================== DRAW
			    void drawFrame() {
			        	final SurfaceHolder holder = getSurfaceHolder();
			            			
			            Canvas c = null;
			            
			            try {
			                c = holder.lockCanvas(); 
			                if (c != null) doDraw(c);
			                
			            } finally {
			                if (c != null) holder.unlockCanvasAndPost(c);			                
			            }	
			            mHandler.removeCallbacks(mDrawWall);			     		            			       
			            if (mVisible) 
			            	mHandler.postDelayed(mDrawWall, 40); //  ms
			     
		        }


			    private void doDraw(Canvas canvas) {	  			    	
			        	if(this.backgroundImage == null )  load(0);
			        	paint.setAlpha(255);
				    	canvas.drawBitmap(this.backgroundImage, xP, 0, paint); // xP from onOffsetsChanged
				    	if(this.c1 == null )  load(1);				    	
				    	canvas.drawBitmap(this.c1, xP+w, 100, paint);				    	
      		    }

			    
		        private void load(int a)
		        {		        			        
		        	Resources mRes = getApplicationContext().getResources(); 				        		            
		        	BitmapFactory.Options options = new BitmapFactory.Options();
		        	
		        	options.inDensity = 320;
		        	options.inTargetDensity = getResources().getDisplayMetrics().densityDpi;	
		        	options.inScaled = true;		        	
		        	switch (a) {
		        	case 0:		        		
		        		this.backgroundImage = BitmapFactory.decodeResource(mRes, R.drawable.back, options); break; // loading the background image
		        	case 1:		        		
		        		this.c1 = BitmapFactory.decodeResource(mRes, R.drawable.c1, options); break; // loading the cloud image
		        
		        	}
		        	this.backgroundImage = Bitmap.createScaledBitmap(this.backgroundImage, wallWidth+50, wallHeight, false); 
		        }
			//====================================== 
		        @Override
		        public void onCreate(SurfaceHolder surfaceHolder) {
			        	super.onCreate(surfaceHolder);
			        	surfaceHolder.setFormat(android.graphics.PixelFormat.RGBA_8888);			        
				    	setTouchEventsEnabled(true);
			           
		        }
		
		        @Override
		        public void onDestroy() {
			            super.onDestroy();
			            mHandler.removeCallbacks(mDrawWall);
		        }
		        @Override
		        public void onVisibilityChanged(boolean visible) {
			            mVisible = visible;
			            if (visible) {				    	
			                drawFrame();
			            } else {
			                mHandler.removeCallbacks(mDrawWall);				   
			            }
		        }
		        @Override
		        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
			           super.onSurfaceChanged(holder, format, width, height);			         
			           drawFrame();			           
		        }
		        @Override
		        public void onSurfaceCreated(SurfaceHolder holder) {
		            	super.onSurfaceCreated(holder);
		        }
		        @Override
		        public void onSurfaceDestroyed(SurfaceHolder holder) {
			            super.onSurfaceDestroyed(holder);
			            mVisible = false;
			            mHandler.removeCallbacks(mDrawWall);
		        }
		        @Override
		        public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) {
		        					            
		            		xP= xPixels; 			            			            				            				            			                       				            				          
		        }
				
		        @Override
		        public void onTouchEvent(MotionEvent event) {			  //===Touch Event      
			            super.onTouchEvent(event);			            
		        }   
		      
		        
 }
}
